﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace desktop.Igrica
{
    public partial class SlovaricaForm : Form
    {
        private List<Slovo> _slova;

        private Bitmap _backBuffer;
        
        private Thread _th;
        System.Windows.Forms.Timer _timer;
        
        private Font _gui_font;
        private Brush _gui_brush;

        private int _lastTime;
        private bool _inGame;
        private int _bodovi = 0;

        public SlovaricaForm()
        {
            InitializeComponent();

            _backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);

            _timer = new System.Windows.Forms.Timer();
            _timer.Interval = 30;
            _timer.Tick += new EventHandler(Timer_Tick);

            _gui_font  = new Font("Comic Sans MS", 20);
            _gui_brush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

            _th = new Thread(new ThreadStart(Thread_Level));

            _slova = new List<Slovo>();

            _lastTime = Environment.TickCount;
            _inGame = true;

            _th.Start();
            _timer.Start();
        }


        private void SlovaricaForm_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(_backBuffer, 0, 0);

            _draw_gui(e.Graphics);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
        }



        private void Timer_Tick(object sender, EventArgs e)
        {
            Graphics g = Graphics.FromImage(_backBuffer);
            g.Clear(Color.FromArgb(255, 255, 255, 255));


            for (int i = 0; i < _slova.Count; i++)
            {
                _slova[i].Update();
                _slova[i].Iscrtaj(g);

                if (_slova[i].Y > ClientSize.Height || _slova[i].IsDead)
                {
                    _bodovi += _slova[i].IsDead ? 10 : -30;
                    _slova[i] = Slovo.NovoSlovo(ClientSize.Width);
                }
            }


            this.Invalidate();
        }



        private void Thread_Level()
        {
            while(_slova.Count < 40 && _inGame )
            {
                _slova.Add(Slovo.NovoSlovo(ClientSize.Width));
                Thread.Sleep(3500);
            }
        }


        
        private void _draw_gui(Graphics g)
        {
            int time = (_inGame ? 1000*60*2 : 1000*10) - (Environment.TickCount - _lastTime);

            if (time < 0)
            {
                time = 0;

                if (!_inGame)
                {
                    _inGame = true;

                    _th = new Thread(new ThreadStart(Thread_Level));
                    _th.Start();

                    _bodovi = 0;
                    _lastTime = Environment.TickCount;
                }
                else
                {
                    _inGame = false;
                    _slova.Clear();

                    _lastTime = Environment.TickCount;
                }
            }

            if ( !_inGame )
            {
                string score = string.Format("Vas skor: {0}", _bodovi);
                g.DrawString(score, _gui_font, _gui_brush, 170, 150);
            }

            string guiText = string.Format("Bodovi: {0}", _bodovi);
            string timeText = string.Format("Time: {0}:{1}:{2}", time / (1000*60), (time / 1000) % 60, time % 1000);

            g.DrawString(guiText, _gui_font, _gui_brush, 290, ClientSize.Height - 50);
            g.DrawString(timeText, _gui_font, _gui_brush, 10, ClientSize.Height - 50);
        }



        private void SlovaricaForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            for (int i = 0; i < _slova.Count; i++)
            {
                if (_slova[i].Karakter == char.ToUpper(e.KeyChar))
                {
                    _slova[i].animate_death();
                }
            }
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            _timer.Stop();
            _th.Abort();
            base.OnClosing(e);
        }
    }
}
